package cn.etynit.basic.service.impl;

import cn.etynit.basic.config.BussinessException;
import cn.etynit.basic.constant.BaseConstant;
import cn.etynit.basic.mapper.RegisterMapper;
import cn.etynit.basic.service.IRegisterService;
import cn.etynit.basic.util.MD5Utils;
import cn.etynit.basic.util.StrUtils;
import cn.etynit.user.domain.Logininfo;
import cn.etynit.user.domain.User;
import cn.etynit.user.dto.EmailRegisterDto;
import cn.etynit.user.dto.PhoneRegisterDto;
import cn.etynit.user.mapper.LogininfoMapper;
import cn.etynit.user.mapper.UserMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class RegisterServiceImpl implements IRegisterService {

    @Autowired
    private RegisterMapper registerMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private LogininfoMapper logininfoMapper;

    private User dtoConverUser(PhoneRegisterDto dto){
        // 盐值
        String salt = StrUtils.getComplexRandomString(32);
        // 密码
        String password = salt+dto.getPassword();
        // 生成密码存入数据库
        String encryPwd = MD5Utils.encrypByMd5(password);
        return User.builder()
                .username(dto.getPhone())
                .phone(dto.getPhone())
                .salt(salt)
                .password(encryPwd)
                .state(1)
                .createtime(new Date())
                .build();
    }
    private Logininfo userConvertLogininfo(User user){
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(user,logininfo);
        return logininfo;
    }

    @Override
    public void phoneRegister(PhoneRegisterDto dto) {
        // 验证参数
        this.verfyParam(dto);
        // 先插入loginfo 在插入user
        User user = dtoConverUser(dto);
        Logininfo logininfo = userConvertLogininfo(user);
        // 添加
        logininfoMapper.add(logininfo);
        user.setLogininfoId(logininfo.getId());
        userMapper.add(user);
    }
    // 手机号注册判断方法
    private void verfyParam(PhoneRegisterDto dto){
        // 判断非空
        // 两次密码是否一致
        if(!dto.getPassword().equals(dto.getPasswordRepeat())){
            throw new BussinessException("两次密码不一致！");
        }
        // 验证码是否正确 / 两次手机号是否一致
        String redisSmsCode = (String)redisTemplate.opsForValue().get(
                String.format(BaseConstant.VerfityCodeCons.REGISTER_SMSCODE_PHONE, dto.getPhone()));
        if (StringUtils.isBlank(redisSmsCode)){
            throw new BussinessException("验证码错误！");
        }
        String[] smsCode = redisSmsCode.split(":");

        if (!smsCode[0].equals(dto.getSmsCode())){
            throw new BussinessException("验证码错误！");
        }
        // 是否注册过
        User user = userMapper.findByPhone(dto.getPhone());
        if (null != user){
            throw new BussinessException("改手机号已注册过！");
        }
    }








    private User emaildtoConverUser(EmailRegisterDto emaildto){
        // 盐值
        String salt = StrUtils.getComplexRandomString(32);
        // 密码
        String password = salt+emaildto.getPassword();
        // 生成密码存入数据库
        String encryPwd = MD5Utils.encrypByMd5(password);
        return User.builder()
                .username(emaildto.getEmail())
                .email(emaildto.getEmail())
                .salt(salt)
                .password(encryPwd)
                .state(1)
                .createtime(new Date())
                .build();
    }
    private Logininfo emailuserConvertLogininfo(User user){
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(user,logininfo);
        return logininfo;
    }

    @Override
    public void emailRegister(EmailRegisterDto emaildto) {
        this.emailVerfyParam(emaildto);
        // 先插入loginfo 在插入user
        User user = emaildtoConverUser(emaildto);
        Logininfo logininfo = emailuserConvertLogininfo(user);
        // 添加
        logininfoMapper.add(logininfo);
        user.setLogininfoId(logininfo.getId());
        userMapper.add(user);

    }

    // 邮箱注册判断方法
    private void emailVerfyParam(EmailRegisterDto emaildto) {
        // 判断两次密码是否一致
        if(!emaildto.getPassword().equals(emaildto.getPasswordRepeat())){
            throw new BussinessException("两次密码不一致！");
        }
        // 是否注册过
        User user = userMapper.findByEmail(emaildto.getEmail());
        if (null != user){
            throw new BussinessException("改邮箱已注册过！");
        }
    }
}
